﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using System.Web;
namespace Microsoft.MCSUK.SPSiteDirectory2010
{
    public class SiteDirectorySiteManager : LayoutsPageBase
    {
        protected System.Web.UI.WebControls.Literal ErrorMessageLiteral;
        protected Microsoft.SharePoint.WebControls.SiteAdministrationSelector SiteSelector;
        protected System.Web.UI.WebControls.Button OKButton;
        protected System.Web.UI.WebControls.Label SiteDirectoryWebAppLabel;
        protected System.Web.UI.WebControls.Label SiteDirectoryStatusLabel;
        protected System.Web.UI.WebControls.Button DisableButton;
        
        protected override void OnLoad(EventArgs e)
        {
            GlobalMethods globalMethods = new GlobalMethods();
            GlobalSolutionSettings globalSolutionSettings = new GlobalSolutionSettings().GetOrCreateGlobalSolutionSettings(SPFarm.Local);
            if (!IsPostBack)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        if (globalMethods.DoesSiteExist(globalSolutionSettings.SiteDirectoryURL))
                        {
                            SiteDirectoryStatusLabel.Text = string.Concat(HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "ADMIN_SiteDirectoryManager_StatusSection_SiteDirectoryActivated").ToString(), "<A href='", globalSolutionSettings.SiteDirectoryURL, "'>", globalSolutionSettings.SiteDirectoryURL, "</A>");
                        }
                        else
                        {
                            SiteDirectoryStatusLabel.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "ADMIN_SiteDirectoryManager_StatusSection_SiteDirectoryNotActivated").ToString();
                            //clean-up settings
                            globalSolutionSettings.Delete();
                            globalSolutionSettings.Unprovision();
                            globalSolutionSettings.Uncache();
                        }
                    });
                }
                catch (Exception ex)
                {
                    ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_GenericUIErrorText").ToString();
                    globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
                }
            }
        }
        protected void DisableButton_OnClick(object sender, EventArgs e)
        {
            GlobalMethods globalMethods = new GlobalMethods();
            GlobalSolutionSettings globalSolutionSettings = new GlobalSolutionSettings().GetOrCreateGlobalSolutionSettings(SPFarm.Local);
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    if (globalMethods.DoesSiteExist(globalSolutionSettings.SiteDirectoryURL))
                    {
                        using (SPSite siteDirectorySite = new SPSite(globalSolutionSettings.SiteDirectorySiteGuid))
                        {
                            using (SPWeb siteDirectoryWeb = siteDirectorySite.OpenWeb(globalSolutionSettings.SiteDirectoryWebGuid))
                            {
                                siteDirectoryWeb.AllowUnsafeUpdates = true;
                                siteDirectoryWeb.Features.Remove(GlobalConstants.AddSiteListingContentTypeFeatureID, true);
                                siteDirectoryWeb.Features.Remove(GlobalConstants.SiteListingsFeatureID, true);
                            }
                        }
                    }
                    else
                    {
                        ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "ADMIN_SiteDirectoryManager_CannotDeactivateFeatures").ToString();
                    }
                    globalSolutionSettings.Delete();
                    globalSolutionSettings.Unprovision();
                    globalSolutionSettings.Uncache();
                });
                SPUtility.Redirect(GlobalConstants.CentralAdminRedirectURL, SPRedirectFlags.Trusted | SPRedirectFlags.DoNotEndResponse, this.Context);
            }
            catch (Exception ex)
            {
                ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_GenericUIErrorText").ToString();
                globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
            }
        }
        protected void OKButton_OnClick(object sender, EventArgs e)
        {
            GlobalMethods globalMethods = new GlobalMethods();
            GlobalSolutionSettings globalSolutionSettings = new GlobalSolutionSettings().GetOrCreateGlobalSolutionSettings(SPFarm.Local);
            try
            {
                if (Page.IsValid)
                {
                    if (SiteSelector.CurrentItem != null)
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            using (SPSite selectedSite = new SPSite(SiteSelector.CurrentItem.Url))
                            {
                                using (SPWeb selectedWeb = selectedSite.OpenWeb())
                                {
                                    globalSolutionSettings.SiteDirectorySiteGuid = selectedSite.ID;
                                    globalSolutionSettings.SiteDirectoryWebGuid = selectedWeb.ID;
                                    globalSolutionSettings.SiteDirectoryURL = selectedWeb.Url;
                                    globalSolutionSettings.Update();
                                    selectedWeb.Features.Add(GlobalConstants.SiteListingsFeatureID, true);
                                    selectedWeb.Features.Add(GlobalConstants.AddSiteListingContentTypeFeatureID, true);
                                }
                            }
                        });
                    }
                    SPUtility.Redirect(GlobalConstants.CentralAdminRedirectURL, SPRedirectFlags.Trusted | SPRedirectFlags.DoNotEndResponse, this.Context);
                }
                else
                { 
                    ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "ADMIN_Generic_InvalidPageControls").ToString();
                }
            }
            catch (Exception ex)
            {
                ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_GenericUIErrorText").ToString();
                globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
            }
        }
    }
}
